package com.beijing.student.action;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController;

import com.beijing.student.pojo.Student;

public class EditStudent extends AbstractCommandController {

	
	public EditStudent() {
		this.setCommandClass(Student.class);
	}

	//调用service保存学生信息
	//这里仅测试将页面的参数提交到命令对象中
	@Override
	protected ModelAndView handle(HttpServletRequest request,HttpServletResponse response,
			Object command, BindException exception)	throws Exception {
		ModelAndView modelAndView = new ModelAndView();
		Student student = (Student)command;
		System.out.println(student);
		modelAndView.addObject("student", student);
		modelAndView.setViewName("student/success");
		return modelAndView;
	}
	
	//注册一个属性编辑器,将student类中的birthday属性进行日期转换，类型和birthday一致
	@Override
	protected void initBinder(HttpServletRequest request,ServletRequestDataBinder binder) throws Exception {
		binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
	}

}